/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.example.netcracker.ui;

import ru.example.netcracker.ui.listeners.OnClickTableItemListener;
import ru.example.netcracker.ui.listeners.OnClickMenuItemListener;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import ru.example.netcracker.model.entity.Task;

/**
 *
 * @author Nikolay
 */
public final class TaskTable extends JTable implements OnClickMenuItemListener {

    public class TableMouseListener extends MouseAdapter {
        
        @Override
        public void mouseReleased(MouseEvent e) {
            Point point = e.getPoint();
            int currentRow = rowAtPoint(point);
            setRowSelectionInterval(currentRow, currentRow);
            if(e.getButton() == 3)
                menu.show(TaskTable.this, point.x, point.y);
        }
    }
        
    public class DataTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return rows.size();
        }

        @Override
        public int getColumnCount() {
            return colls.length;
        }
        
        public Task getRow(int rowIndex) {
            return rows.get(rowIndex);
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Task task = rows.get(rowIndex);
            Date date = task.getDate();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd.MMMM.yyyy");
            String formatDate = format.format(date);
            String[] split = formatDate.split(" ");
            String timeStr = split[0];
            String dateStr = split[1];
            switch(columnIndex) {
                case 0:
                    return task.getName();
                case 1:
                    return timeStr;
                case 2:                    
                    return dateStr;                
                default:
                    return "";                                                    
            }
        }
        
        @Override
        public String getColumnName(int column) {
            return colls[column];
        }
    }
    private final String[] colls = {"Тема", "Время", "Число"};
    
    private List<Task> rows = new ArrayList<>();    
    private OnClickTableItemListener listener;
    private TablePopupMenu menu;
    
    public TaskTable() {
        this(new ArrayList());
    }
    
    public TaskTable(List<Task> rows) {
        super();
        this.rows = rows;
        menu = new TablePopupMenu(this);
        this.addMouseListener(new TableMouseListener());        
        setModel(new DataTableModel());
    }

    public List<Task> getRows() {
        return rows;
    }

    public void setRows(List<Task> rows) {
        this.rows = rows;
        menu.setVisible(false);
    }
    
    public void addOnClickTableItemListener(OnClickTableItemListener listener) {
        this.listener = listener;
    }
    
    public void removeOnClickTableItemListener() {
        listener = null;
    }
    
    @Override
    public void onClickChange() {
        if(listener != null) {
            int selectedRow = this.getSelectedRow();
            DataTableModel model = (DataTableModel) this.getModel();        
            listener.onClickChange(model.getRow(selectedRow));
        }
    }
    
    @Override
    public void onClickDelete() {
        if(listener != null) {
            int selectedRow = this.getSelectedRow();
            DataTableModel model = (DataTableModel) this.getModel();        
            listener.onClickDelete(model.getRow(selectedRow));
        }
    }
}
